package com.example.demosecurityjwtlogin.controllers;




import com.example.demosecurityjwtlogin.models.Bean;
import com.example.demosecurityjwtlogin.security.services.BeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/test1")
public class BeanController {
    @Autowired
    BeanService beanService;

    @PostMapping("/")
    public Bean addBook(@RequestBody Bean bean){
    beanService.addBook(bean);
    return bean;
}
    @PostMapping("/all")
    public List<Bean> addAllBook(@RequestBody List<Bean> books){
        beanService.addAllBook(books);
        return books;
    }
    @GetMapping("/user")
    @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
    public List<Bean> getBooksByuserStartingWith(String user){
        return beanService.getBooksByuserStartingWith(user);
    }
    @GetMapping("/balance")
    @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
    public List<Bean> getBooksBybalanceGreaterThan(Integer balance){
        return beanService.getBooksBybalanceGreaterThan(balance);
    }
    @GetMapping("/getMaxIdBook")
    @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
    public Bean getMaxIdBook(){
        return beanService.getMaxIdBook();
    }
    @GetMapping("/getBooksByIdAndAuthor")
    @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
    public List<Bean> getBooksByIdAndAuthor(String user, Long id){
        return beanService.getBooksByIdAndAuthor(user,id);
    }
    @GetMapping("/getBookByIdAndName")
    @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
    public List<Bean> getBookByIdAndName(String name, Long id){
        return beanService.getBooksByIdAndName(name,id);
    }
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
    public String putBook(@RequestBody Bean book){
        return beanService.putBook(book);
    }
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('MODERATOR') ")
    public String delete(@PathVariable Long id){
        return beanService.delete(id);
    }
    @GetMapping("/{id}")
    @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
    public Page<Bean> getBookByPage(Pageable pageable){
        return  beanService.getBookByPage(pageable);
    }
    @GetMapping("/page")
    @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
    public List<Bean> getBookBypage(int pageNo, int size){
        return beanService.getBookBypage(pageNo,size);
    }
}


